<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
    }

    // 用户注册
    public function register()
    {
        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header');
            $this->load->view('user/register');
            $this->load->view('templates/footer');
        }
        else
        {
            $username = $this->input->post('username', TRUE);
            $password = md5($this->input->post('password'));
            $email = $this->input->post('email');

            if ($this->user_model->add_user($username, $password, $email))
            {
                $this->load->view('templates/header');
                $this->load->view('project/list');
                $this->load->view('templates/footer');
            }
        }
    }

    // 用户登录
    public function login()
    {
         if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header');
            $this->load->view('user/login');
            $this->load->view('templates/footer');
        }
        else
        {
            $this->user_model->login($this->input->post('username'));
           
            redirect('project/prolist');
        }
    }

    // 退出登录
    public function logout()
    {
        $this->session->sess_destroy();
        
        $this->load->view('templates/header');
        $this->load->view('user/login');
        $this->load->view('templates/footer');
    }


    // 自定义验证用户名函数
    function username_exists($username)
    {
        if ($this->user_model->get_by_username($username))
        {
             $this->form_validation->set_message('username_exists', '用户名已被占用');
             return FALSE;
        }
        return TRUE;
    }

    // 自定义验证邮件函数
    function email_exists($email)
    {
        if ($this->user_model->email_exists($email))
        {
            $this->form_validation->set_message('email_exists', '该邮箱已被使用');
            return FALSE;
        }
        return TRUE;
    }

    // 自定义验证登录用户名函数
    function username_check($username)
    {
        if ($this->user_model->get_by_username($username))
        {
             return TRUE;
        }
        else
        {
            $this->form_validation->set_message('username_check', '用户名不存在!');
            return FALSE;
        }
    }

    // 自定义验证登录密码函数
    function password_check($password)
    {
        $username = $this->input->post('username');
        $password = md5($password);

        if ($this->user_model->password_check($username, $password))
        {
            return TRUE;
        }
        else 
        {
            $this->form_validation->set_message('password_check', '用户名或密码不正确!');
            return FALSE;
        }
    }


}
